﻿using UnityEngine;
using System.Collections;
using System.IO;
using System.Net;
using System;

namespace MyAssetBundles {
    public class DownloadFolder : BaseFolder, IFolderEnumerator {
        public string downloadFileName;
        public long downloadByteTotal;
        public IEnumerator GenerateEnumerator (string path, IFolderProgress progress)
        {
            SetProgress(progress, downloadByteTotal);
          //  AddAutoDeleteFile(downloadFileName);
            Debug.Log("download start");

         //   yield return DownloadWithWWW(path);

            yield return DownloadWithBreakpoint(path);
        }
        

        protected IEnumerator DownloadWithBreakpoint (string path)
        {
            Uri uri = null;
            if (!Uri.TryCreate(path, UriKind.RelativeOrAbsolute, out uri))
                yield break;

            string downloadPath = Path.GetDirectoryName(downloadFileName);
            if (!Directory.Exists(downloadPath))
            {
                Directory.CreateDirectory(downloadPath);
            }

            FileInfo file = new FileInfo(downloadFileName);
            FileStream fStream = file.Open(FileMode.OpenOrCreate, FileAccess.Write);
            long processNum = fStream.Length;
            if (processNum >= downloadByteTotal)
            {
                fStream.Close();
                yield break;
            }
            SetProcessNum(processNum);
            fStream.Seek(0, SeekOrigin.End);

            Debug.Log("--- download start");
            yield return new WaitForEndOfFrame();
            HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;
            request.Method = "GET";
            request.AddRange((int)processNum);
            Stream stream = request.GetResponse().GetResponseStream();
            byte[] buffer = new byte[1024];
            int length = 0;
            while (processNum < downloadByteTotal)
            {
                yield return new WaitForEndOfFrame();
                while (true)
                {
                    length = stream.Read(buffer, 0, buffer.Length);
                    fStream.Write(buffer, 0, length);
                    processNum += length;
                    if (length < 1024) break;
                }
                SetProcessNum(processNum);
            }

            stream.Close();
            stream.Dispose();
            fStream.Flush();
            fStream.Close();
            yield return new WaitForEndOfFrame();
        }

        protected IEnumerator DownloadWithWWW (string path)
        {
            WWW w = new WWW(path);
            while (!w.isDone)
            {
                SetProgress(w.progress);
                yield return new WaitForEndOfFrame();
            }
            yield return w;

            string downloadPath = Path.GetDirectoryName(downloadFileName);
            if (!Directory.Exists(downloadPath))
            {
                Directory.CreateDirectory(downloadPath);
            }

            FileInfo file = new FileInfo(downloadFileName);

            FileStream fStream = file.Create();
            fStream.Write(w.bytes, 0, w.size);
            fStream.Close();

            Debug.Log(file.FullName);
        }

        protected override void OnClean ()
        {
            Debug.Log("download, name: " + downloadFileName);
        }
    }
}


